/*
 * Main.java
 *
 * Created on 8 de noviembre de 2007, 02:18 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package persistence;

import com.sun.demo.jpa.Player;
import com.sun.demo.jpa.Team;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author DSILVA
 */
public class Main {
    
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistencePU");
    
    /** Creates a new instance of Main */
    public Main() {
    }
    private void guardar() {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            Team[] teams =new Team[] {
                new Team("Los Angeles Dodgers", "National"),
                new Team("San Francisco Giants", "National"),
                new Team("Anaheim Angels", "American"),
                new Team("Boston Red Sox", "American")
            };
            
            Player[] players = new Player[] {
                // name, number, last quoted statement
                new Player("Lowe", "Derek", 23, "The sinker's been good to me."),
                new Player("Kent", "Jeff", 12, "I wish I could run faster."),
                new Player("Garciaparra", "Nomar", 5,"No, I'm not superstitious at all.")};
            
            for(Team team: teams) {
                em.persist(team);
            }
            
            
            for(Player player:players){
                player.setTeam(teams[0]);
                teams[0].addPlayer(player);
                em.persist(player);
            }
            
            
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().guardar();
    }
    
    
}
